import { createRouter, createWebHistory } from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import  users from '../components/User/users.vue'
import  roles from '../components/Roles/roles.vue'
import  rights from '../components/Roles/rights.vue'
const routes = [
  {
    path: '/',
    redirect: '/login'

  },
  {
    path: '/login',
    component:login
  },
  {
    path:'/home',
    component: home,
    redirect: '/welcome',
    children:[
      {path:'/welcome',component:welcome},
      {path:'/users',component:users},
      {path:'/roles',component:roles},
      {path:'/rights',component:rights}

    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


//设置一下路由守卫
router.beforeEach((to,from,next)=>{
  //登录，不拦截
  if(to.path ==='/login') return next();
  let token = sessionStorage.getItem('token');
  if(!token) return next('/login');
  next();
})
export default router
